using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ns
{
    ///<summary>
    ///
    ///</summary>

    
    public class MonoSingleton<T> : MonoBehaviour where T : ns.MonoSingleton<T>
    {
        private static T instance;
        public static T Instance
        {
            get
            {
                if (instance==null)
                {
                    instance=GameObject.FindObjectOfType<T>();
                    if (instance == null)
                    {
                        new GameObject("MonoSingleton of "+typeof(T)).AddComponent<T>();
                    }
                    else
                    {
                        instance.Init();
                    }
                }

                return instance; 
            }
        }
        public void Awake()
        {
            if (instance==null)
            {
                instance=this as T;
                Init();
            }
        }
        public virtual void Init()
        {

        }
    }
}
